﻿Imports System.Data.SqlClient

Public Class frmRegistraRoles

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim Rol As New clsRolesDescuentos
        Rol.Nombre = txtNombre.Text
        Rol.Origen = txtOrigen.Text
        Rol.Destino = txtDestino.Text
        Rol.HoraInicio = txtInicio.Text
        Rol.HoraTermino = txtTermino.Text
        If radioMarcas.Checked = True Then
            Rol.TipoDescuento = 1
        Else
            Rol.TipoDescuento = 0
        End If
        Rol.Marcas = txtMarcas.Text
        Rol.PorcentajeMarcas = txtPorcentaje.Text
        Rol.PorcentajeDescuento = txtDescuento.Text
        Rol.TipoDia = txtDia.Text
        If IdServicio = 0 Then
            Rol.Insertar()
        Else
            Rol.IdServicio = IdServicio
            Rol.Actualizar()
        End If

        CargaDestino()
        CargaOrigen()
    End Sub

    Private Sub frmRegistraRoles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaDestino()
        CargaOrigen()

        Buscar()



    End Sub
    Sub Buscar()
        Dim Rol As New clsRolesDescuentos
        Rol.IdServicio = IdServicio
        Rol.Buscar()

        txtOrigen.Text = Rol.Origen
        txtDestino.Text = Rol.Destino
        txtInicio.Text = Rol.HoraInicio
        txtTermino.Text = Rol.HoraTermino
        txtNombre.Text = Rol.Nombre
        txtMarcas.Text = Rol.Marcas
        txtPorcentaje.Text = Rol.PorcentajeMarcas
        txtDescuento.Text = Rol.PorcentajeDescuento
        txtDia.Text = Rol.TipoDia

        If Rol.TipoDescuento = 1 Then
            radioPorcentaje.Checked = False
            radioMarcas.Checked = True
        Else
            radioPorcentaje.Checked = True
            radioMarcas.Checked = False
        End If
    End Sub

    Private Sub radioMarcas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioMarcas.CheckedChanged
        If radioMarcas.Checked = True Then
            txtMarcas.Enabled = True
            txtDescuento.Enabled = False
            txtPorcentaje.Enabled = False
            txtMarcas.Focus()
            txtMarcas.SelectAll()

        Else
            txtMarcas.Enabled = False
            txtDescuento.Enabled = True
            txtPorcentaje.Enabled = True
        End If
    End Sub

    Private Sub radioPorcentaje_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioPorcentaje.CheckedChanged
        If radioPorcentaje.Checked = True Then
            txtMarcas.Enabled = False
            txtDescuento.Enabled = True
            txtPorcentaje.Enabled = True
            txtPorcentaje.Focus()
            txtPorcentaje.SelectAll()

        Else
            txtMarcas.Enabled = True
            txtDescuento.Enabled = False
            txtPorcentaje.Enabled = False
        End If
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        IdServicio = 0
        Buscar()

    End Sub
    Sub CargaOrigen()
        txtOrigen.AutoCompleteCustomSource = Nothing

        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String
        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "Select Origen FROM RolesDescuentos GROUP BY Origen"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                txtOrigen.AutoCompleteCustomSource.Add(rdBuscar("Origen").ToString.Trim)

            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub
    Sub CargaDestino()
        txtDestino.AutoCompleteCustomSource = Nothing

        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String
        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "Select Destino FROM RolesDescuentos GROUP BY Destino"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                txtDestino.AutoCompleteCustomSource.Add(rdBuscar("Destino").ToString.Trim)

            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub

    Private Sub txtOrigen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtOrigen.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtDestino.Focus()
            txtDestino.SelectAll()

        End If
    End Sub

    Private Sub txtOrigen_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOrigen.TextChanged

    End Sub

    Private Sub txtDestino_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDestino.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtInicio.Focus()
            txtInicio.SelectAll()

        End If
    End Sub

    Private Sub txtDestino_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDestino.TextChanged

    End Sub

    Private Sub txtInicio_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInicio.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtTermino.Focus()
            txtTermino.SelectAll()

        End If
    End Sub

    Private Sub txtInicio_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInicio.TextChanged

    End Sub

    Private Sub txtTermino_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTermino.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtNombre.Focus()
            txtNombre.SelectAll()

        End If
    End Sub

    Private Sub txtTermino_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTermino.TextChanged

    End Sub

    Private Sub txtNombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNombre.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtPorcentaje.Focus()
            txtPorcentaje.SelectAll()

        End If
    End Sub

    Private Sub txtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNombre.TextChanged

    End Sub

    Private Sub txtPorcentaje_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPorcentaje.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtDescuento.Focus()
            txtDescuento.SelectAll()

        End If
    End Sub

    Private Sub txtPorcentaje_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPorcentaje.TextChanged

    End Sub

    Private Sub txtDescuento_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDescuento.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtDia.Focus()
            txtDia.SelectAll()

        End If
    End Sub

    Private Sub txtDescuento_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDescuento.TextChanged

    End Sub

    Private Sub txtMarcas_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMarcas.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            txtPorcentaje.Focus()
            txtPorcentaje.SelectAll()

        End If
    End Sub

    Private Sub txtMarcas_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMarcas.TextChanged

    End Sub
End Class